<template>
  <div>
    <BackBtn />
    <div class="header">
      <div class="title">{{ title }}</div>
    </div>
    <div class="content">
      <van-form @submit="onSubmit">
        <van-field
          readonly
          clickable
          name="area"
          :value="value"
          label="地区选择"
          placeholder="点击选择省市区"
          @click="showArea = true"
          v-if="$route.params.type === '地址添加' ? true : false"
        />
        <van-popup v-model="showArea" position="bottom">
          <van-area
            :area-list="areaList"
            :columns-placeholder="['请选择', '请选择', '请选择']"
            @confirm="onConfirm"
            @cancel="showArea = false"
          />
        </van-popup>
        <van-field
          v-model="addrsss"
          name="address"
          label="地址"
          placeholder="请输入地址(街道、门牌号)"
          :rules="[{ required: true }]"
        />
        <van-field
          v-model="nick"
          name="nick"
          label="收货人"
          placeholder="收货人姓名"
          :rules="[{ required: true }]"
        />
        <van-field
          v-model="phone"
          type="number"
          name="phone"
          label="手机号"
          placeholder="请输入手机号"
          :rules="[{ required: true }]"
        />
        <van-field name="default" label="设为默认地址" input-align="right">
          <template #input>
            <van-switch v-model="defaultChecked" size="20" />
          </template>
        </van-field>
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit"
            >保存并使用</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { reqGetCityInfo, reqGetAddress, updateAddress } from "@/api";
import { Toast } from "vant";
export default {
  data() {
    return {
      value: "",
      showArea: false,
      areaList: {},
      addrsss: "",
      nick: "",
      phone: "",
      defaultChecked: false,
    };
  },
  computed: {
    title() {
      return this.$route.params.type;
    },
  },
  methods: {
    onConfirm(values) {
      this.value = values
        .filter((item) => !!item)
        .map((item) => item.name)
        .join("/");
      this.showArea = false;
    },
    showPopup() {
      this.show = true;
    },
    async getCityInfo() {
      let res = await reqGetCityInfo();
      if (res.status === 200) {
        this.areaList = res.data;
      }
    },
    async getAddress() {
      let id = this.$store.state.login.userInfo.id;
      let res = await reqGetAddress({ id });
      if (res.status === 200) {
        this.$store.state.user.allUserInfo.address = JSON.parse(
          res.data.address
        );
      }
    },
    async onSubmit(values) {
      let oldAddress = this.$store.state.user.allUserInfo.address;
      if (this.$route.params.type === "地址添加") {
        // 地址添加
        let newAddress = {
          address: values.area.split("/").join("") + values.address,
          id: this.$store.state.login.userInfo.id,
          nick: values.nick,
          phone: values.phone,
          default: values.default,
        };
        // 设新增地址为默认
        if (values.default === true) {
          oldAddress.forEach((item) => {
            item.default = false;
          });
          oldAddress.push(newAddress);
        } else {
          oldAddress.push(newAddress);
        }
        let addStr = JSON.stringify(oldAddress);
        let obj = {
          id: this.$store.state.login.userInfo.id,
          address: addStr,
        };
        let res = await updateAddress(obj);
        if (res.status === 200) {
          Toast("添加地址成功");
          this.$router.push("/center/address");
        } else {
          Toast("添加地址失败");
        }
      } else {
        // 地址修改
        let id = this.$store.state.login.userInfo.id;
        Object.assign(values, { id });
        let index = this.$route.query.index;
        let allAddress = this.$store.state.user.allUserInfo.address;
        allAddress.splice(index, 1, values);
        oldAddress.forEach(item => {
          item.default = false;
        })
        oldAddress[index].default = this.defaultChecked;
        let addStr = JSON.stringify(allAddress);
        let obj = {
          id: this.$store.state.login.userInfo.id,
          address: addStr
        };
        let res = await updateAddress(obj);
        if (res.status === 200) {
          Toast("修改地址成功");
          this.$router.back();
        } else {
          Toast("修改地址失败");
        }
      }
    },
  },
  mounted() {
    this.getAddress();
    this.getCityInfo();
    if (this.$route.params.type === "地址编辑") {
      let index = this.$route.query.index;
      let add = this.$store.state.user.allUserInfo.address[index];
      this.addrsss = add.address;
      this.nick = add.nick;
      this.phone = add.phone;
      this.defaultChecked = add.default;
    }
  },
};
</script>

<style>
.van-switch--on {
  background-color: #143ea9;
}
.van-button--info {
  background-color: #143ea9;
  border: 1px solid #143ea9;
}
</style>

<style lang="less" scoped>
.header {
  height: 50px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #e8e7e7;
}
.content {
  padding: 10px;
  width: 100%;
  box-sizing: border-box;
}
</style>
